package com.example.controller;

import com.example.dao.WxbGoodRepository;
import com.example.model.WxbCustomer;
import com.example.model.WxbGood;
import com.example.model.WxbMemeber;
import com.example.service.EsGoodService;
import com.example.service.GoodService;
import com.example.service.WxbCustomerService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/good")
public class GoodController {

    @Autowired
    private GoodService goodService;
    @Autowired
    private WxbGoodRepository wxbGoodRepository;
    @Autowired
    private WxbCustomerService wxbCustomerService;


    @Autowired
    private EsGoodService esGoodService;



    @RequestMapping("/goodlist")
    public String getgoodlist(Model model) {

        WxbMemeber wxbMemeber = (WxbMemeber) SecurityUtils.getSubject().getPrincipal();
        model.addAttribute("wxbMemeber",wxbMemeber);
        //初始加载页面使用查询全部，并将结果保存到es中
        List<WxbGood> goods=goodService.findAll();

        for (WxbGood good : goods) {
            WxbCustomer customer = wxbCustomerService.findByCustomerId(good.getCustomerId());
            good.setLevel(customer.getLevel());
            good.setTaocan(setMeal(good));
            good.setTagsStr(setStagsStr(good));
            good.setCustomer(wxbCustomerService.findByCustomerId(good.getCustomerId()));
            good.setLevel(wxbCustomerService.findByCustomerId(good.getCustomerId()).getLevel());
            good.setLevelStr(setLevelStr(good));
            good.setSkuList(setSkuList(good));
            wxbGoodRepository.save(good);
        }
        model.addAttribute("goods",goods);



        List<WxbCustomer> wxbCustomerList = wxbCustomerService.findAll();

        model.addAttribute("wxbCustomers",wxbCustomerList);


        return "redirect:/good/esQueryList";


    }

    @RequestMapping("/esQueryList")
    public String esQueryList(String customerId,String typeId, String sortType,String alisaName,Integer row,Integer size,Model model){

        WxbMemeber wxbMemeber = (WxbMemeber) SecurityUtils.getSubject().getPrincipal();
        model.addAttribute("wxbMemeber",wxbMemeber);
        Page<WxbGood> page = esGoodService.esQueryList(customerId, typeId, sortType, alisaName,row,size);

        model.addAttribute("goods",page.getContent());
        model.addAttribute("customerId",customerId);
        model.addAttribute("typeId",typeId);
        model.addAttribute("sortType",sortType);
        model.addAttribute("alisaName",alisaName);
        model.addAttribute("row",row);
        model.addAttribute("size",size);
        //总条数
        model.addAttribute("pageNum",page.getTotalPages());
        //总页数
        model.addAttribute("totalElements",page.getTotalElements());

        List<WxbCustomer> wxbCustomerList = wxbCustomerService.findAll();

        model.addAttribute("wxbCustomers",wxbCustomerList);


//        System.out.println(goods);
        return "产品及文案";
    }
    //定义一个将套餐信息设置进去的方法
    private List<String> setMeal(WxbGood good){
        List<String> taocan = new ArrayList<>();
        if(StringUtils.isNoneBlank(good.getSkuTitle()) ){
            String[] title = good.getSkuTitle().split("\\|");
            String[] price = good.getSkuPrice().split("\\|");
            String[] pMoney = good.getSkuPmoney().split("\\|");

            for(int i=0;i<title.length;i++){
                String s ="" ;
                s=title[i]+"：￥单价"+price[i]+"/提成"+pMoney[i];
                taocan.add(s);
            }
        }


        return taocan;
    }
    //定义一个将套餐存为list的方法
    private List setSkuList(WxbGood good){
        List<List<String>>skuList = new ArrayList<>();
        if(StringUtils.isNoneBlank(good.getSkuTitle()) ){
            String[] title = good.getSkuTitle().split("\\|");
            String[] price = good.getSkuPrice().split("\\|");
            String[] pMoney = good.getSkuPmoney().split("\\|");

            for(int i=0;i<title.length;i++){
               List<String> sku = new ArrayList<>();
               sku.add(title[i]);
               sku.add(price[i]);
               sku.add(pMoney[i]);
               skuList.add(sku);
            }
        }
        return skuList;
    }








//    "1" 热销爆款"2"女粉推荐"3"男粉推荐"4"人气新品"5"男女通用
//定义一个将类型设置进去的方法
    private List<String> setStagsStr(WxbGood good){
        String[] tag ={ "热销爆款","女粉推荐","男粉推荐","人气新品","男女通用"};

        List<String> tagsStr = new ArrayList<>();
        if( StringUtils.isNoneBlank(good.getTags())){
            String[] sta = good.getTags().split(",");
            for (String s : sta) {
                tagsStr.add(tag[Integer.parseInt(s)-1]);
            }
        }
        return tagsStr;

    }

//    0 非认证商户 1认证商户V1 2认证商户V2  3认证商户V3
//定义一个将商户等级设置进去的方法
    private String setLevelStr(WxbGood wxbGood){
        String[] leStr = {"非认证商户","认证商户V1","认证商户V2","认证商户V3"};
        return leStr[wxbGood.getLevel()];
    }

    @RequestMapping("/goodSku")
    public String goodSku( String goodId,Model model){
        System.out.println(goodId);
        WxbMemeber wxbMemeber = (WxbMemeber) SecurityUtils.getSubject().getPrincipal();
        model.addAttribute("wxbMemeber",wxbMemeber);

        WxbGood goodSku = esGoodService.findGoodSku(goodId);
        model.addAttribute("goodSku",goodSku);


        return "产品及文案-详情";
    }

}
